package app;

import java.awt.event.KeyEvent;
import java.io.IOException;

import libs.game.sokoban.PersonPath;
import libs.game.sokoban.SokobanGame;
import libs.game.sokoban.SokobanGamePlan;
import libs.game.sokoban.SokobanGamePlanFactory;
import libs.game.sokoban.SokobanSolver;
import libs.window.GamePlanPanel;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on 22.2.2009, 14:03:33
 */
/**
 *
 * @author papi
 */
public class MainWindow extends javax.swing.JFrame {

    private SokobanGame game;

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gamePlanPanel = new GamePlanPanel();
        jButton1 = new javax.swing.JButton();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        gamePlanPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                gamePlanPanelMousePressed(evt);
            }
        });
        gamePlanPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                gamePlanPanelMouseMoved(evt);
            }
        });
        gamePlanPanel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                gamePlanPanelKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout gamePlanPanelLayout = new javax.swing.GroupLayout(gamePlanPanel);
        gamePlanPanel.setLayout(gamePlanPanelLayout);
        gamePlanPanelLayout.setHorizontalGroup(
            gamePlanPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 536, Short.MAX_VALUE)
        );
        gamePlanPanelLayout.setVerticalGroup(
            gamePlanPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 379, Short.MAX_VALUE)
        );

        jButton1.setText("Zkusit přesunout");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
        });

        jFormattedTextField1.setEnabled(false);

        jMenu1.setText("Hra");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Nová hra");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem2.setText("Konec");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Akce");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Nápověda");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(gamePlanPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(gamePlanPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void gamePlanPanelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_gamePlanPanelKeyPressed
        this.doLayout();
        int movesBefore = game.getGamePlan().numberOfMoves();
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                //System.out.println("LEFT");
                this.game.getGamePlan().move(SokobanGamePlan.MOVE_WEST);
                break;
            case KeyEvent.VK_RIGHT:
                //System.out.println("RIGHT");
                this.game.getGamePlan().move(SokobanGamePlan.MOVE_EAST);
                break;
            case KeyEvent.VK_UP:
                //System.out.println("UP");
                this.game.getGamePlan().move(SokobanGamePlan.MOVE_NORTH);
                break;
            case KeyEvent.VK_DOWN:
                //System.out.println("DOWN");
                this.game.getGamePlan().move(SokobanGamePlan.MOVE_SOUTH);
                break;
        }
        if (game.getGamePlan().numberOfMoves() > movesBefore) {
            game.computePaths();
            this.gamePlanPanel.setPersonPath(game.getPersonPath());
            this.gamePlanPanel.setBoxPath(game.getBoxPaths());
        }
        this.gamePlanPanel.repaint(evt);
        if (this.game.isLocked()) {
            this.jFormattedTextField1.setText("Hra již nelze dohrát.");
        } else if (this.game.isSolved()) {
            this.jFormattedTextField1.setText("Hra je dohrána");
        } else {
            this.jFormattedTextField1.setText("Hra lze zřejmě dohrát.");
        }
    }//GEN-LAST:event_gamePlanPanelKeyPressed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        this.gamePlanPanelKeyPressed(evt);
    }//GEN-LAST:event_formKeyPressed

    private void gamePlanPanelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gamePlanPanelMouseMoved
        this.gamePlanPanel.setBoxPath(game.getBoxPaths());
        this.gamePlanPanel.setPersonPath(game.getPersonPath());
        this.gamePlanPanel.setCursorField(evt);
        this.gamePlanPanel.repaint();
    }//GEN-LAST:event_gamePlanPanelMouseMoved

    private void gamePlanPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gamePlanPanelMousePressed
        this.gamePlanPanel.markField(evt);
        this.gamePlanPanel.repaint();
    }//GEN-LAST:event_gamePlanPanelMousePressed

    private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton1KeyPressed
        this.gamePlanPanelKeyPressed(evt);
    }//GEN-LAST:event_jButton1KeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (gamePlanPanel.getMarkedFields().size() > 0) {
            try {
                SokobanGamePlan newPlan = SokobanGamePlanFactory.changeBoxesAndTargets(
                        (SokobanGamePlan) game.getGamePlan(), gamePlanPanel.getMarkedFields());
                jFormattedTextField1.setText("Zkouším přesunout.");
                switch (new SokobanSolver().isSoluble((SokobanGamePlan) game.getGamePlan(), newPlan)) {
                    case -1:
                        jFormattedTextField1.setText("Bedny nelze takto přesunout.");
                        break;
                    case 0:
                        jFormattedTextField1.setText("Nejsem si jistý, zda lze bedny přesunout.");
                        break;
                    case 1:
                        game = new SokobanGame(newPlan);
                        gamePlanPanel.setPlan((SokobanGamePlan) game.getGamePlan());
                        jFormattedTextField1.setText("Bedny přesunuty.");
                        break;
                }
            } catch (IllegalArgumentException e) {
                jFormattedTextField1.setText("Byla označena špatná políčka.");
            }
            gamePlanPanel.resetMarkedFields();
            gamePlanPanel.repaint();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws IOException {
        MainWindow win = new MainWindow();
        win.setVisible(true);
        win.game = new SokobanGame("src/data/microcosmos-15.sokoban");
        try {
            win.gamePlanPanel.setPlan((SokobanGamePlan) win.game.getGamePlan());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private GamePlanPanel gamePlanPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    // End of variables declaration//GEN-END:variables
}


